﻿using System.Runtime.InteropServices;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint;
using FlexnetConsultants.CookieNotification.Resources;

namespace FlexnetConsultants.CookieNotification.Settings
{
        [Guid("E7224D91-B3FD-491D-972C-04A448AFF711")]
        public class FCCookieNotificationSettings: SPPersistedObject
        {
            private const string OBJECT_NAME = "FCCookieNotificationSettings";


            [Persisted]
            private string _CookieName;


            [Persisted]
            private string _CookieMessageBody;

            [Persisted]
            private string _CookieMessageTitle;


            [Persisted]
            private string _AcceptCookieLinkTitle;


            [Persisted]
            private bool _Enabled;

            [Persisted]
            private bool _DebugMode;

            protected override bool HasAdditionalUpdateAccess()
            {
                return true;
            }

            public FCCookieNotificationSettings()
            {
            }

            private FCCookieNotificationSettings(SPPersistedObject parent)
                : base(OBJECT_NAME, parent)
            {
            }

            public static FCCookieNotificationSettings Local
            {
                get
                {

                    SPPersistedObject parent = SPFarm.Local;
                    var obj = parent.GetChild<FCCookieNotificationSettings>(OBJECT_NAME);
                    if (obj == null)
                    {
                        obj = new FCCookieNotificationSettings(parent);
                        
                        obj.CookieName="FCCookieAccept";
                        obj.Enabled = false; // Initially not activated
                        obj.DebugMode = false;
                        obj.CookieMessageBody= GetLocalizedString("CookieMessage"); // SPUtility.GetLocalizedString("$Resources:FCCookieNotification", "UserListResource", 1033);
                        obj.AcceptCookieLinkTitle =GetLocalizedString("CookieLinkTitle");

                        obj.Update(true);
                    }

                    return obj;
                }
            }

            private static string GetLocalizedString(string keyName)
            {
                string resourceText = SPUtility.GetLocalizedString(
               "$Resources:" + keyName,
               "FCCookieNotification", 1033);

                return resourceText;
            }


            public string CookieName
            {
                get { return _CookieName; }
                set { _CookieName = value; }
            }

            public string CookieMessageTitle
            {
                get { return _CookieMessageTitle; }
                set { _CookieMessageTitle = value; }
            }


            public string CookieMessageBody
            {
                get { return _CookieMessageBody; }
                set { _CookieMessageBody = value; }
            }


            public string AcceptCookieLinkTitle
            {
                get { return _AcceptCookieLinkTitle; }
                set { _AcceptCookieLinkTitle = value; }
            }

            public bool Enabled
            {
                get { return _Enabled; }
                set { _Enabled = value; }
            }

            public bool DebugMode
            {
                get { return _DebugMode; }
                set { _DebugMode = value; }
            }

            

        }

    }

